﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dukou.Security.Domain;
using Dukou.Security.BizService;

namespace Dukou.Security
{
    public class DukouContext
    {
        /// <summary>
        /// 
        /// </summary>
        public static ICurrentUserContext CurrentUserContext { get; set; }

        public static IPassportService PassportService { get; set; }

        public static string SessionID
        {
            get
            {
                return CurrentUserContext.SessionID;
            }
        }

        public static string IP
        {
            get
            {
                return CurrentUserContext.IP;
            }
        }

        public static string UserData
        {
            get
            {
                return CurrentUserContext.UserData;
            }
        }

        public static User CurrentUser
        {
            get
            {
                if (string.IsNullOrEmpty(UserData))
                {
                    CurrentUserContext.Init();
                    if (string.IsNullOrEmpty(UserData))
                    {
                        return User.Guest;
                    }
                }
                LogonInfo logonInfo = PassportService.FindByIdentifier(UserData);
                if (logonInfo == null)
                {
                    PassportService.Logout();
                    return null;
                }
                return logonInfo.User;
            }
        }
    }
}
